package com.hctforgreen.greenservice.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Toast;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.ctr.UpdateController;
import com.hctforgreen.greenservice.utils.DownloadBookLstInitFinishUtil;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.WifiFreshStateStoreUtil;

public class WifiUpdateService extends Service {
	public static final String MY_NAME = "com.hct.greeserver.wifiauto_update";

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// showSucToast();
		Log.w("WifiUpdateService----onStart", "-------------------");
		if (WifiFreshStateStoreUtil.get(getApplicationContext())) {
			Log.d("WifiUpdateService", "开始自动更新");
			Log.i("hh", "开始自动更新");
			getAllSeries();
		}
		super.onStart(intent, startId);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}

	public void onLowMemory() {
	};

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	private Thread updateThread = new Thread() {
		public void run() {
			// 更新所有系列信息

		};
	};

	/**
	 * 1、所有系列信息
	 */
	private void getAllSeries() {

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					Log.d("WifiUpdateService", "getAllSeries更新成功");
					getAllMachines();

					break;

				}
			}

		};

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					//test wush
					UpdateController controller = new UpdateController(
							getApplicationContext());

					HctResult result = controller.getAllSeries(false);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 2、所有机型信息
	 */
	private void getAllMachines() {

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					Log.d("WifiUpdateService", "getAllMachines更新成功");
					getAllColumns();

					break;

				}
			}

		};

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					UpdateController controller = new UpdateController(
							getApplicationContext());

					HctResult result = controller.getAllMachines(false);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 调试维修条目
	 * 
	 * @param convertView
	 * @param bTopRefresh
	 */
	private void getAllColumns() {

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:

					if (DownloadBookLstInitFinishUtil
							.getInitFinished(getApplicationContext())) {

						return;
					}
					Log.d("WifiUpdateService", "getAllColumns更新成功");
					getAllBooksWithAllChildSeriesAndDbSerieName();

					break;

				}
			}

		};

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					UpdateController controller = new UpdateController(
							getApplicationContext());

					HctResult result = controller.getAllColumns(false);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}

	private void showSucToast() {
		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(LayoutInflater.from(getApplicationContext()).inflate(
				R.layout.wifi_update_toast, null));
		toast.show();
	}

	private void getAllBooksWithAllChildSeriesAndDbSerieName() {
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS: // 更新成功
					Log.d("WifiUpdateService", "getAllBooksWithAllChildSeriesAndDbSerieName更新成功");
					showSucToast();
					break;
				}
			}

		};

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					UpdateController controller = new UpdateController(
							getApplicationContext());

					HctResult result = controller
							.getAllBooksWithAllChildSeriesAndDbSerieName(false);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}
	
	
}
